package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.framework.exception.BizException;
import com.molichuxing.services.infrastructure.bean.InvoiceApplyBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.InvoiceApplyMapper;
import com.molichuxing.services.infrastructure.dto.request.create.InvoiceApplyCreateDto;
import com.molichuxing.services.infrastructure.dto.response.InvoiceApplyDto;
import com.molichuxing.services.infrastructure.service.InvoiceApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 发票申请 服务实现类
 * </p>
 *
 * @author zhaopenghui
 * @since 2019-09-11
 */
@Service("invoiceApplyService")
public class InvoiceApplyServiceImpl implements InvoiceApplyService {
    @Autowired
    InvoiceApplyMapper invoiceApplyMapper;


    /**
     * 通过id获取申请信息
     *
     * @param invoiceApplyIdList
     * @return
     */
    @Override
    public Map<Integer, InvoiceApplyDto> getInvoiceApplyMap(List<Integer> invoiceApplyIdList) throws Exception {
        if (null == invoiceApplyIdList || invoiceApplyIdList.size() == 0) {
            return null;
        }
        List<InvoiceApplyBean> invoiceApplyList = invoiceApplyMapper.getListByIds(invoiceApplyIdList);

        Map<Integer, InvoiceApplyDto> invoiceApplyMap = null;
        if (invoiceApplyList != null && invoiceApplyList.size() > 0) {
            List<InvoiceApplyDto> invoiceApplyDtoList = Convert.toInvoiceApplyDtoList(invoiceApplyList);
            invoiceApplyMap = invoiceApplyDtoList.stream().collect(Collectors.toMap(InvoiceApplyDto::getInvoiceApplyId, a -> a, (k1, k2) -> k1));
        }
        return invoiceApplyMap;
    }

    /**
     * 根据订单或者用户获取发票申请id
     *
     * @param orderCode
     * @param userId
     * @return
     * @throws Exception
     */
    @Override
    public List<Integer> getInvoiceApplyIds(Long orderCode, Integer userId) throws Exception {
        if (null == orderCode && null == userId) {
            return null;
        }

        return invoiceApplyMapper.getIdsByOrderOrUser(orderCode, userId, null);
    }

    /**
     * 根据订单或者用户获取发票申请id
     *
     * @param orderCode
     * @param userId
     * @return
     * @throws Exception
     */
    @Override
    public List<Integer> getInvoiceApplyIds(Long orderCode, Integer userId, Integer invoiceTitleType) throws Exception {
        if (null == orderCode && null == userId && null == invoiceTitleType) {
            return null;
        }

        return invoiceApplyMapper.getIdsByOrderOrUser(orderCode, userId, invoiceTitleType);
    }

    /**
     * 发票申请
     *
     * @param invoiceApplyCreateDto
     * @return
     * @throws Exception
     */
    public Integer createInvoiceApply(InvoiceApplyCreateDto invoiceApplyCreateDto) throws Exception {
        Assert.notNull(invoiceApplyCreateDto, "发票申请失败");

        InvoiceApplyBean invoiceApplyBean = Convert.toInvoiceInfoBizDto(invoiceApplyCreateDto);
        invoiceApplyMapper.create(invoiceApplyBean);
        return invoiceApplyBean.getId();
    }


}
